const app = getApp();
const api = app.globalData.api;
Page({
  data: {
    inputValue: '',
    userInfo: {},
    result: '这里是翻译后的内容...',
    errorMsg: '',
    typeList: ['汉译英', '英译汉'],
    typeIndex: 0,
    typeContent: '汉译英',
    from: 'zh',
    to: 'en',
    isVisable: false,//是否可见
    myCollectList: [],//我的收藏
    myNewNoteList: [],//我的生词本
    myLoveList: [],//我的喜欢
    isCollect: false,//是否已经收藏
    isNewNote: false,//是否已经加入生词本
    isLove: false,//是否已经加入我的喜欢
  },
  onShow: function () {
    let isLoginStore = wx.getStorageSync('isLogin');
    this.setData({
      'userInfo': wx.getStorageSync('loginUser')
    });
    this.getCollectNewNoteLove();//获取我的收藏，我的喜欢，我的生词本
    console.log("isLogin==>", isLoginStore);
    if (isLoginStore) {
      wx.navigateTo({
        url: '/pages/index/index',
      });
    } else {
      wx.navigateTo({
        url: '/pages/login/login',
      });
    }
  },

  //获取我的收藏，我的喜欢，我的生词本
  getCollectNewNoteLove: function() {
    let that = this;
    // console.log("uid==>" + this.data.userInfo.id);
    wx.request({
      url: api + '/user/getCollectNewNoteLove/' + this.data.userInfo.id,
      method: 'GET',
      success: (res) => {
        console.log(res.data);
        if (res.data.code == 200) {
          //成功
          // console.log("mycollectList==> ", res.data.data.myCollectList);
          that.setData({
            'myCollectList': res.data.data.myCollectList,
            'myNewNoteList': res.data.data.myNewNoteList,
            'myLoveList': res.data.data.myLoveList
          });
        } else {
          wx.showToast({
            title: '数据获取出现异常',
            icon: 'none'
          });
        }
      },
      fail: (err) => {
        console.log(err);
        wx.showToast({
          title: '数据获取请求错误',
          icon: 'none'
        });
      }
    });
  },

  prevContent: function () {
    let index = this.data.typeIndex - 1;
    if (index < 0) {
      index = this.data.typeList.length - 1;
    }
    this.setData({
      typeIndex: index,
      typeContent: this.data.typeList[index],
    });
    if (this.typeContent == '汉译英') {
      this.setData({
        from: 'zh',
        to: 'en'
      });
    } else {
      this.setData({
        from: 'en',
        to: 'zh'
      });
    }
  },

  nextContent: function () {
    let index = this.data.typeIndex + 1;
    if (index >= this.data.typeList.length) {
      index = 0;
    }
    this.setData({
      typeIndex: index,
      typeContent: this.data.typeList[index]
    });
    if (this.typeContent == '汉译英') {
      this.setData({
        from: 'zh',
        to: 'en'
      });
    } else {
      this.setData({
        from: 'en',
        to: 'zh'
      });
    }
  },
  inputChange: function (e) {
    this.setData({
      'inputValue': e.detail.value,
      'isVisable': false,
    });
  },
  sendRequest: function () {
    var q = this.data.inputValue
    var from = this.data.from
    var to = this.data.to
    console.log("from==>" + from + "; to==>" + to);
    if (q === '') {
      this.setData({
        errorMsg: '输入的内容不能为空'
      });
      return;
    }
    this.setData({
      errorMsg: ''
    });
    let that = this;
    wx.request({
      url: 'http://localhost:8282/translate/sentence',
      method: 'POST',
      data: {
        q: q,
        from: from,
        to: to
      },
      success: (res) => {
        var result = res.data.trans_result[0].dst;
        var ress = res.data;
        console.log("res==>" + result);
        that.setData({
          'result': result,
          'isVisable': true,
        });
        that.setIsValue();//修改是否收藏，是否已加入生词本，是否已加入喜欢的布尔值
      },
      fail: (err) => {
        console.log(err);
        wx.showToast({
          title: '翻译请求失败',
          icon: 'none'
        });
      }
    });
  },

  //修改是否收藏，是否已加入生词本，是否已加入喜欢的布尔值
  setIsValue: function() {
    let isCollect = false;
    let isNewNote = false;
    let isLove = false;
    let myCollectList = this.data.myCollectList;
    let myNewNoteList = this.data.myNewNoteList;
    let myLoveList = this.data.myLoveList;
    let inputValue = this.data.inputValue;
    console.log(myLoveList, inputValue);
    myCollectList.forEach(function (item) {
      if (item.word.toLowerCase() == inputValue.toLowerCase() || item.translation.toLowerCase() == inputValue.toLowerCase()) {
        isCollect = true
      }
    });
    myNewNoteList.forEach(function (item) {
      if (item.word.toLowerCase() == inputValue.toLowerCase() || item.translation.toLowerCase() == inputValue.toLowerCase()) {
        isNewNote = true
      }
    });
    myLoveList.forEach(function (item) {
      if (item.word.toLowerCase() == inputValue.toLowerCase() || item.translation.toLowerCase() == inputValue.toLowerCase()) {
        isLove = true
      }
    });
    let that = this;
    that.setData({
      'isCollect': isCollect,
      'isNewNote': isNewNote,
      'isLove': isLove
    });
    console.log(isCollect, isNewNote, isLove);
  },


  addWordList(e) {
    let type = e.currentTarget.dataset.type;//类型（1、收藏，2、生词本，3、喜欢）
    let uid = this.data.userInfo.id;//用户id
    let url = '/user/operatorWord';//要确认是加入还是取消
    console.log("type==>", type);
    let that = this;
    //发起修改请求
    wx.request({
      url: api + url + '/' + uid + '/' + type,
      method: 'POST',
      data: {
        word: that.data.inputValue,
        translation: that.data.result
      },
      success: (res) => {
        console.log(res.data);
        if (res.data.code == 200) {
          that.getCollectNewNoteLove();//重新获取
          setTimeout(()=>{
            that.setIsValue();//修改是否收藏，是否已加入生词本，是否已加入喜欢的布尔值
          },500);
        } else {
          wx.showToast({
            title: '操作失败',
            icon: 'none'
          });
        }
      },
      fail: (err) => {
        console.log(err);
        wx.showToast({
          title: '修改请求出错',
          icon: 'none'
        });
      }
    });
  }
})
